package flc.ast.activity;

import android.graphics.Bitmap;
import android.view.View;
import android.widget.SeekBar;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.bumptech.glide.Glide;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityIdpicCompressBinding;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.ProgressConvertUtil;
import stark.common.basic.utils.Str2NumUtil;

public class IDPicCompressActivity extends BaseAc<ActivityIdpicCompressBinding> {

    public static String imgPath;
    private float mResolutionScale = 1.0f;
    private int rate = 100;
    private double size;

    @Override
    protected int onCreate() {
        return R.layout.activity_idpic_compress;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivIDPicCompressBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvStartCompress.setOnClickListener(this);

        mDataBinding.sbProgress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mResolutionScale = ProgressConvertUtil.progress2value(progress, 0.1f, 1.0f, 100);
                mDataBinding.tvProgress.setText(progress + "%");
                rate = progress;
                updateCompressFileSize();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        Glide.with(mContext).load(imgPath).into(mDataBinding.ivCompressImg);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.tvStartCompress://开始压缩
                save();
                break;
        }
    }

    @Override
    protected void initData() {
        long length = FileUtils.getLength(imgPath);
        String formattedValue = String.format("%.2f", ((double) length / 1024));
        Bitmap bitmap = ImageUtils.getBitmap(imgPath);
        mDataBinding.tvPixel.setText(bitmap.getWidth() + "x" + bitmap.getHeight() + getString(R.string.unit_px));
        mDataBinding.tvAfterPixel.setText(bitmap.getWidth() + "x" + bitmap.getHeight() + getString(R.string.unit_px));
        mDataBinding.tvSize.setText(formattedValue);
        mDataBinding.tvAfterSize.setText(formattedValue);
        size = Str2NumUtil.parse(formattedValue);
    }

    private void updateCompressFileSize() {
        double newSize = size * mResolutionScale;
        String formattedValue = String.format("%.2f", newSize);
        mDataBinding.tvAfterSize.setText(formattedValue);
    }

    private void save() {
        byte[] bytes = ImageUtils.compressByQuality(ImageUtils.getBitmap(imgPath), rate);
        Bitmap bitmap = ImageUtils.bytes2Bitmap(bytes);
        //保存到相册
        ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
        //保存到最近文件
        String filePath = FileUtil.generateFilePath(Content.myCompress, getString(R.string.unit_png));
        ImageUtils.save(bitmap, filePath, Bitmap.CompressFormat.PNG);
        CompressActivity.isSingle = true;
        CompressActivity.imgPath = imgPath;
        startActivity(CompressActivity.class);
    }

}